/*
 * @Author: hyq
 * @LastEditors: hyq
 * @Description:
 * @Date: 2023-12-05 12:51:27
 */
// theme.js

import { defineStore } from 'pinia'
export const useModifyTheme = defineStore('theme', {
  state: () => {
    return {
      isDark: uni.getStorageSync('isDark') || false, // 控制主题开关
      themeClass: uni.getStorageSync('theme') || 'dark' // 主题名
    }
  },
  getters: {
    determineTheTheme(state) {
      return state.isDark ? 'light' : 'dark' //开关为true返回'light',否则返回'dark'
    }
  },
  actions: {
    modifyThemeHanlder(e) {
      //
      console.log(e)
      this.isDark = e
      this.themeClass = this.determineTheTheme
      // 把值存本地
      uni.setStorageSync('theme', this.themeClass)
      uni.setStorageSync('isDark', this.isDark)
    },
    setNavigationBarColorHandler() {
      const { themeClass } = this
      // 这里主要是改变tabbar和导航栏的颜色
      const darkNavigationBar = {
        frontColor: '#000000',
        backgroundColor: '#ffffff',
        animation: {
          duration: 0,
          timingFunc: 'easeIn'
        }
      }

      const lightNavigationBar = {
        frontColor: '#ffffff',
        backgroundColor: '#000000',
        animation: {
          duration: 0,
          timingFunc: 'easeIn'
        }
      }

      const tabBarStyle = {
        color: themeClass === 'dark' ? '#000000' : '#ffffff',
        selectedColor: '#76C8F9',
        backgroundColor: themeClass === 'dark' ? '#ffffff' : '#000000',
        borderStyle: 'black'
      }

      uni.setNavigationBarColor(
        themeClass === 'dark'
          ? (darkNavigationBar as any)
          : (lightNavigationBar as any)
      )
      const tabBarPages = [
        'pages/index/index',
        // 'pages/shopping/shopping',
        // 'pages/consultant/consultant',
        // 'pages/cart/cart',
        'pages/mine/mine'
      ] //  TabBar 页面路径

      // 判断当前页面是否是 TabBar 页面
      const currentPages = getCurrentPages()
      const currentPage = currentPages[currentPages.length - 1]
      if (tabBarPages.includes(currentPage.route as string)) {
        uni.setTabBarStyle(tabBarStyle)
      }
    }
  }
})
